package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import models.MedidaAluno;
import models.Usuario;


public class DAOMedidaAluno {

	public Set<MedidaAluno> select() throws Exception {
		Set<MedidaAluno> objs = new HashSet<MedidaAluno>();
		try {

			String query = "select idUsuario, idMedida, data, valor from medidaAluno";

			Statement statement = DAOConnection.getConnection().createStatement();

			ResultSet resultSet = statement.executeQuery(query);

			while(resultSet.next()) {
				MedidaAluno obj = new MedidaAluno();
				obj.idUsuario = resultSet.getInt(1);
				obj.idMedida = resultSet.getInt(2);
				obj.data = DAOConnection.date2StringPTBR(resultSet.getDate(3));
				obj.valor = resultSet.getFloat(4);
				objs.add(obj);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return objs;
	}

	public Set<MedidaAluno> selectDatas(int idUsuario) throws Exception {
		Set <MedidaAluno> objs = new HashSet<MedidaAluno>();
		try {

			String query = "Select idUsuario, data FROM MedidaAluno where idUsuario = ? GROUP BY idUsuario, data";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(query);
			statement.setInt(1, idUsuario);
			ResultSet resultSet = statement.executeQuery();
			MedidaAluno obj;
			while(resultSet.next()) {
				obj = new MedidaAluno();
				obj.idUsuario = resultSet.getInt(1);
				obj.data = DAOConnection.date2StringPTBR(resultSet.getDate(2));
				objs.add(obj);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		
		return objs;
	}
	
	public Usuario selectXIdEData(MedidaAluno medidaA) throws Exception {
		Usuario usuario = new Usuario();
		try {

			String query = "SELECT ma.idUsuario, ma.idMedida, ma.data, ma.valor, m.nome, u.nome " +
					"FROM MedidaAluno ma, Medida m, Usuario u " +
					"WHERE ma.idUsuario = ? AND ma.data = ? " +
					"	AND ma.idUsuario = u.idUsuario " +
					"	AND ma.idMedida = m.idMedida";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(query);
			statement.setInt(1, medidaA.idUsuario);
			statement.setString(2, DAOConnection.date2StringSQL(DAOConnection.string2DateHTML(medidaA.data)));

			ResultSet resultSet = statement.executeQuery();
			MedidaAluno obj;
			while(resultSet.next()) {
				obj = new MedidaAluno();
				obj.idUsuario = resultSet.getInt(1);
				obj.idMedida = resultSet.getInt(2);
				obj.data = DAOConnection.date2StringPTBR(resultSet.getDate(3));
				obj.valor = resultSet.getFloat(4);
				obj.medida.nome = resultSet.getString(5);
				usuario.nome = resultSet.getString(6);
				usuario.idUsuario = obj.idUsuario;
				usuario.medidas.add(obj);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		
		return usuario;
	}

	public void insert(Usuario usu) throws Exception {
		try {
			String insert = "insert into MedidaAluno(idUsuario, idMedida, data, valor) values(?, ?, ?, ?)";
			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(insert);

			Date agora = new Date();

			for (MedidaAluno medidaA : usu.medidas) {

				statement.setInt(1, usu.idUsuario);
				statement.setInt(2, medidaA.idMedida);
				statement.setString(3, DAOConnection.date2StringSQL(agora));
				statement.setFloat(4, medidaA.valor);

				statement.executeUpdate();
			}


		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}

	}

	public void update(MedidaAluno f) throws Exception {
		try {

			String update = "UPDATE MedidaAluno set valor = ? WHERE idUsuario = ? AND idMedida = ? AND data = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(update);

			statement.setFloat(1, f.valor);
			statement.setInt(2, f.idUsuario);
			statement.setInt(3, f.idMedida);
			statement.setString(4, DAOConnection.date2StringSQL(DAOConnection.string2DateHTML(f.data)));

			statement.executeUpdate();
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}

	}

	public void delete(MedidaAluno f) throws Exception {
		try {

			String delete = "delete from MedidaAluno where idUsuario = ? AND idMedida = ? AND data = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(delete);

			statement.setInt(1, f.idUsuario);
			statement.setInt(2, f.idMedida);
			statement.setString(3, DAOConnection.date2StringSQL(DAOConnection.string2DateHTML(f.data)));

			statement.executeUpdate();
		} catch(SQLException e) {
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
	}

}
